package ModelLayer;
import java.util.*;


/**
 * Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Person
{
    // instance variables - replace the example below with your own
    private int id;
    private String name;
    private String address;
    private int postalCode;
    private String city;
    private String phone;
    private HashSet<Loan> loans;
    /**
     * Constructor for objects of class Person
     */
    public Person()
    {
       id=0;
       name=" ";
       address=" ";
       postalCode=0;
       city=" ";
       phone=" ";
       loans=new HashSet<>();
    }

    
    
    
     public HashSet<Loan> readLoans()
    {
        return loans;
    }
    
     public int readSize()
    {
        return loans.size();
    }
    
    public void create(int id, String name, String address, int postalCode, String city, String phone)
    {
        this.id=id;
        this.name=name;
        this.address=address;
        this.postalCode=postalCode;
        this.city=city;
        this.phone=phone;
    }
    
   
    
    public boolean createLoan(Copy copy, int id, String borrowDate, String period)
    {
        if(loans.size()!=0)
        {
            Iterator<Loan> it=loans.iterator();
            while(it.hasNext())
            {
                Loan existingLoan=it.next();
                if(existingLoan.readID()==id)
                    return false;
                    else
                    {
                        Loan loan=new Loan();
                        loan.createLoan(id,borrowDate,period);
                        loan.updatePerson(this);
                        loan.updateCopy(copy);
                        loan.updateState(false);
                        loans.add(loan);
                        System.out.println("Loan created for the person!");
                    }
            }
            return true;
        }
        else
        {
            Loan loan=new Loan();
            loan.createLoan(id,borrowDate,period);
            loan.updatePerson(this);
            loan.updateCopy(copy);
            loan.updateState(false);
            loans.add(loan);
            System.out.println("Loan created for the person!");
        }
        return false;
    }
    
    public Boolean deleteLoan(int id)
    {
        Iterator<Loan> it = loans.iterator();
        while(it.hasNext()) 
        {
            Loan loan = it.next();
                if(loan.readID()==id)
                    return true;
               else
               {
                    loans.remove(loan);
                    System.out.println("FAAAAAAAAAAAAAAAAAA!");
                }
        }
        return false;
    }
    
    public int readID()
    {
        return id;    
    }
    
    public void updateID(int id)
    {
        this.id=id;    
    }
    
    public String readName()
    {
        return name;
    }
    
    public void updateName(String name)
    {   
        this.name=name;
    }
    
    public String readAddress()
    {
        return address;
    }
    
    public void updateAddress(String address)
    {   
        this.address=address;
    }
    
    public int readPostalCode()
    {
        return postalCode;
    }
    
    public void updatePostalCode(int postalCode)
    {   
        this.postalCode=postalCode;
    }
    
    public String readCity()
    {
        return city;
    }
    
    public void updateCity(String city)
    {   
        this.city=city;
    }
    
    public String readPhone()
    {
        return phone;
    }
    
    public void updatePhone(String phone)
    {   
        this.phone=phone;
    }
    
}
